

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jibble.pircbot.*;

public class FlueBot extends PircBot {

	private IMRP db;
	boolean repeat = false;

	public FlueBot() {
		this.setName("ComplimentBot2");
//		this.setVerbose(true);
		try {
			this.connect("irc.lessthan3.net");
			this.joinChannel("#zubac2");
			db = new IMRP();
		} catch (IOException ex) {
			Logger.getLogger(FlueBot.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IrcException ex) {
			Logger.getLogger(FlueBot.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public void onMessage(String channel, String sender, String login, String hostname, String messageRaw) {

//		if (!sender.equals(this.getNick())) {
//			if (messageRaw.toLowerCase().startsWith(this.getNick().toLowerCase() + " ")) {
//				String message = messageRaw.toLowerCase().trim().substring(this.getNick().length() + 1);
//
//				//Nick testing
//				String regex = "change nick to";
//				Pattern pattern = Pattern.compile(regex + " \\w+");
//				Matcher matcher = pattern.matcher(message);
//				if (matcher.find()) {
//					System.out.println("Found " + matcher.group() + " " + matcher.start() + " " + matcher.end());
//					System.out.println(matcher.group().substring(regex.length() + 1));
//					this.changeNick(matcher.group().substring(regex.length() + 1));
//				} // end Nick testing
//				else {
//
//					// Repeat test
//					if (message.equals("repeat")) {
//						repeat = true;
//					} else {
//						if (message.equals("stop repeating")) {
//							repeat = false;
//						} else {
//							// end Repeat test
//							System.out.println(message);
//							if (message.contains("join #") && message.length() > 8) {
//								System.out.println("Trying to join");
//								this.joinChannel(message.substring(5));
//							} else {
//								if (message.contains("disconnect")) {
//									this.partChannel(channel, "Some douchbag(" + sender + ") told me to leave...");
//								} else {
//									if (repeat) {
//										sendMessage(channel, messageRaw);
//
//									}
//								}
//							}
//						}
//					}
//				}
//			} else {
//				if (repeat) {
//					sendMessage(channel, messageRaw);
//				}
//			}
//		}


		checkForHelp(channel, messageRaw);
		checkForNameChange(messageRaw);
		checkForCharacterPut(channel, sender, messageRaw);
		checkForCharacterGet(channel, messageRaw);
		checkForJoinRequest(channel, messageRaw);
		checkForOtherCharacterPut(channel, messageRaw);
	}

	private void checkForHelp(String channel, String message) {
		if (message.toCharArray().equals(this.getNick() + " help")) {
			sendAction(channel, "help comes here!");
		}
	}

	private void checkForNameChange(String message) {
		String regex = this.getNick() + " change nick to";
		Pattern pattern = Pattern.compile(regex + " \\w+");
		Matcher matcher = pattern.matcher(message);

		if (matcher.find()) {
			System.out.println("Found " + matcher.group() + " " + matcher.start() + " " + matcher.end());
			System.out.println(matcher.group().substring(regex.length() + 1));
			this.changeNick(matcher.group().substring(regex.length() + 1));
		}
	}

	private void checkForCharacterGet(String channel, String message) {
		String regex = "whois";
		Pattern pattern = Pattern.compile(regex + " \\w+");
		Matcher matcher = pattern.matcher(message);
//		System.out.println(message);
		if (matcher.find()) {
			System.out.println("WHOIS");
			String user = message.substring(regex.length() + 1);
			String character = db.getCharacter(user);
			sendMessage(channel, user + " is " + character);
		}
	}

	private void checkForCharacterPut(String channel, String sender, String message) {
		String regex = "I am";
		Pattern pattern = Pattern.compile(regex + " \\w+");
		Matcher matcher = pattern.matcher(message);
		if (matcher.find()) {
			String character = message.substring(regex.length() + 1);
			db.setCharacter(sender, character);
			sendMessage(channel, sender + " is now " + character);
		}
	}

	private void checkForJoinRequest(String channel, String message) {
		if (message.contains("join #") && message.length() > (this.getNick().length() + 8)) {
			this.joinChannel(message.substring(getNick().length() + 5));
		}
	}

	public void onNickChange(String oldNick, String login, String hostname, String newNick) {
		super.onNickChange(oldNick, login, hostname, newNick);
		System.out.println(oldNick + " - " + login + " - " + hostname + " - " + newNick);
		db.userRenamed(oldNick, newNick);
	}

	public static void main(String[] args) {
		new FlueBot();
	}

	private void checkForOtherCharacterPut(String channel, String message) {
		String regex = "\\w+ (is|are) \\w+";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(message);
		if (matcher.find()) {
			String s = matcher.group();
			String[] blocks = s.split(" ");
			db.setCharacter(blocks[0], blocks[2]);
			sendMessage(channel, blocks[0] + " is now " + blocks[2]);
		}
	}
}
